//
//  QYXFindMoreSingleVC.m
//  OpenYourEyes
//
//  Created by dllo on 16/6/7.
//  Copyright © 2016年 Google AdWords. All rights reserved.
//

#import "QYXFindMoreSingleVC.h"
#import "QYXDailyTableViewCell.h"
#import "QYXDailyItemListModel.h"
#import "QYXDailyDetailViewController.h"
#import "AppDelegate.h"

@interface QYXFindMoreSingleVC ()<UITableViewDelegate, UITableViewDataSource>

/** TableView */
@property (nonatomic, strong) UITableView *tableView;
/** 传递数据数组 */
@property (nonatomic, strong) NSMutableArray *dataSourceArr;
@end

@implementation QYXFindMoreSingleVC

- (void)dealloc
{
    self.tableView.delegate = nil;
    self.tableView.dataSource = nil;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self creatTableView];
}



#pragma mark - 创建TableView
- (void)creatTableView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 49 - 64 - 30) style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.rowHeight = 240 *KFitHeight;
    [self.tableView registerClass:[QYXDailyTableViewCell class] forCellReuseIdentifier:@"QYXDailyTableViewCell"];
}


#pragma mark - Delegate

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    QYXDailyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"QYXDailyTableViewCell" forIndexPath:indexPath];
    
    cell.model = self.dataSourceArr[indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    QYXDailyDetailViewController *detailVC = [QYXDailyDetailViewController new];
    detailVC.listDataSource = self.dataSourceArr;
    //    detailVC.modelSection = indexPath.section;
    detailVC.modelRow = indexPath.row;
    
    AppDelegate *tempDelegate = [UIApplication sharedApplication].delegate;
    tempDelegate.tabBar.backView.hidden = YES;
    [self.navigationController pushViewController:detailVC animated:YES];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSourceArr.count;
}

- (void)setUrl:(NSString *)url
{
    if (_url != url) {
        _url = url;
    }
    self.dataSourceArr = [NSMutableArray array];

    [self getData];
}


#pragma mark - 请求数据
- (void)getData
{
    [QYXNetTool getNetWithUrl:_url urlBody:nil header:nil response:QYXJSON success:^(id result) {
        
        for (NSDictionary *d in result[@"itemList"]) {
            
            QYXDailyItemListModel *model = [[QYXDailyItemListModel alloc] initWithDic:d[@"data"]];
            [self.dataSourceArr addObject:model];
        }
        
            [self.tableView reloadData];
            } failure:^(NSError *error) {
    }];
}

@end
